using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using SHDocVw;
using System.Runtime.InteropServices;

namespace NappyBar
{
	[Guid("AE07101B-46D4-4a98-AF68-0333EA26E113")]
    [BandObject("Nappy Bar", BandObjectStyle.Horizontal | BandObjectStyle.ExplorerToolbar | BandObjectStyle.TaskbarToolBar, HelpText = "Enable the Nappy Bar.")]
    //[BandObject("Nappy Bar", BandObjectStyle.Horizontal | BandObjectStyle.TaskbarToolBar, HelpText = "Enable the Nappy Bar.")]
    public class NappyBar : BandObject
    {
        private Button btnPrev;
        private Button btnNext;
        private Button btnPlay;
        private Button btnStop;
		private System.ComponentModel.Container components = null;
        private ToolTip tt;

        bool bInitialized;
        private Napster.INapsterApplication gApp;
        private Napster.IPlayer gPlayer;
        private KeyboardHook kh;
        private Timer gClock;

		public NappyBar()
		{
            try
            {

                bInitialized = false;
                InitializeComponent();
                
                tt = new ToolTip();
                tt.AutoPopDelay = 5000;
                tt.InitialDelay = 1000;
                tt.ReshowDelay = 500;
                tt.ShowAlways = true;
                
                InitializePlayer();

                gClock = new Timer();
                gClock.Interval = 2000;
                gClock.Start();
                gClock.Tick += new EventHandler(Clock_Tick);

                // These are for the Keyboard Hooks, next phase
                //kh = new KeyboardHook(KeyboardHook.Parameters.PassAllKeysToNextApp);
                //kh = new KeyboardHook();
                //kh.KeyIntercepted += new KeyboardHook.KeyboardHookEventHandler(kh_KeyIntercepted);

            }
            catch (Exception e)
            {
                MessageBox.Show("Error :: " + e.Message);
            }
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
            if (kh!= null) kh.Dispose();
            if (gClock != null) gClock = null;
            if (gPlayer != null) gPlayer = null;
            if (gApp != null) gApp = null;
            if (tt != null) tt = null;
		}

        protected void InitializePlayer()
        {
            if (gApp == null) { gApp = new Napster.NapsterApplication(); }
            if (gPlayer == null) { gPlayer = gApp.Player; }
            if (gPlayer != null)
            {
                if (gPlayer.State == Napster.PlayerState.Playing)
                {
                    this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_pause;
                }
                else
                {
                    this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_play;
                }
                InitializeToolTip();
                bInitialized = true;
            }
        }

        private void InitializeToolTip()
        {
            if (gPlayer != null && gPlayer.CurrentTrack != null)
            {
                tt.SetToolTip(this.btnPrev, gPlayer.CurrentTrack.trackTitle);
                tt.SetToolTip(this.btnPlay, gPlayer.CurrentTrack.trackTitle);
                tt.SetToolTip(this.btnStop, gPlayer.CurrentTrack.trackTitle);
                tt.SetToolTip(this.btnNext, gPlayer.CurrentTrack.trackTitle);

            }
        }

        public void Clock_Tick(object sender, EventArgs eArgs)
        {
            if (sender == gClock)
            {
                InitializePlayer();
            }
        }


		#region Component Designer generated code
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NappyBar));
            this.btnStop = new System.Windows.Forms.Button();
            this.btnPlay = new System.Windows.Forms.Button();
            this.btnNext = new System.Windows.Forms.Button();
            this.btnPrev = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnStop
            // 
            this.btnStop.FlatAppearance.BorderSize = 0;
            this.btnStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnStop.Image = ((System.Drawing.Image)(resources.GetObject("btnStop.Image")));
            this.btnStop.Location = new System.Drawing.Point(92, 0);
            this.btnStop.Margin = new System.Windows.Forms.Padding(0);
            this.btnStop.Name = "btnStop";
            this.btnStop.Size = new System.Drawing.Size(34, 28);
            this.btnStop.TabIndex = 3;
            this.btnStop.UseVisualStyleBackColor = true;
            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
            // 
            // btnPlay
            // 
            this.btnPlay.FlatAppearance.BorderSize = 0;
            this.btnPlay.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_play;
            this.btnPlay.Location = new System.Drawing.Point(34, 0);
            this.btnPlay.Margin = new System.Windows.Forms.Padding(0);
            this.btnPlay.Name = "btnPlay";
            this.btnPlay.Size = new System.Drawing.Size(58, 28);
            this.btnPlay.TabIndex = 2;
            this.btnPlay.UseVisualStyleBackColor = true;
            this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
            // 
            // btnNext
            // 
            this.btnNext.FlatAppearance.BorderSize = 0;
            this.btnNext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnNext.Image = ((System.Drawing.Image)(resources.GetObject("btnNext.Image")));
            this.btnNext.Location = new System.Drawing.Point(126, 0);
            this.btnNext.Margin = new System.Windows.Forms.Padding(0);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(34, 28);
            this.btnNext.TabIndex = 1;
            this.btnNext.UseVisualStyleBackColor = true;
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // btnPrev
            // 
            this.btnPrev.FlatAppearance.BorderSize = 0;
            this.btnPrev.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnPrev.Image = ((System.Drawing.Image)(resources.GetObject("btnPrev.Image")));
            this.btnPrev.Location = new System.Drawing.Point(0, 0);
            this.btnPrev.Margin = new System.Windows.Forms.Padding(0);
            this.btnPrev.Name = "btnPrev";
            this.btnPrev.Size = new System.Drawing.Size(34, 28);
            this.btnPrev.TabIndex = 0;
            this.btnPrev.UseVisualStyleBackColor = true;
            this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
            // 
            // NappyBar
            // 
            this.BackColor = System.Drawing.SystemColors.MenuBar;
            this.Controls.Add(this.btnStop);
            this.Controls.Add(this.btnPlay);
            this.Controls.Add(this.btnNext);
            this.Controls.Add(this.btnPrev);
            this.Margin = new System.Windows.Forms.Padding(0);
            this.MinSize = new System.Drawing.Size(161, 29);
            this.Name = "NappyBar";
            this.Size = new System.Drawing.Size(161, 29);
            this.Title = "";
            this.ResumeLayout(false);

		}
		#endregion


        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (!bInitialized)
            {
                InitializePlayer();
            }
            if (gPlayer != null)
            {
                if (gPlayer.State == Napster.PlayerState.Playing)
                {
                    gPlayer.Pause();
                    this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_play;
                }
                else
                {
                    gPlayer.Play();
                    this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_pause;
                }

                InitializeToolTip();
            }
        }

        public override int TranslateAcceleratorIO(ref MSG msg)
        {
            
            if (msg.message == 0x100)//WM_KEYDOWN
                switch (msg.wParam)
                {
                    case (uint)Keys.MediaNextTrack:
                        if (!bInitialized)
                        {
                            InitializePlayer();
                        }
                        gPlayer.Next();
                        break;
                    case (uint)Keys.MediaPreviousTrack:
                        if (!bInitialized)
                        {
                            InitializePlayer();
                        }
                        gPlayer.Previous();
                        break;
                    case (uint)Keys.MediaPlayPause:
                        if (!bInitialized)
                        {
                            InitializePlayer();
                        }

                        if (gPlayer.State == Napster.PlayerState.Playing)
                        {
                            gPlayer.Pause();
                            this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_play;
                        }
                        else
                        {
                            gPlayer.Play();
                            this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_pause;
                        }
                        break;
                }
            return base.TranslateAcceleratorIO(ref msg);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (!bInitialized)
            {
                InitializePlayer();
            }
            if (gPlayer != null)
            {
                gPlayer.Stop();
                this.btnPlay.Image = global::NappyBar.Properties.Resources.nb_play;
                InitializeToolTip();
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (!bInitialized)
            {
                InitializePlayer();
            }
            if (gPlayer != null)
            {
                gPlayer.Previous();
                InitializeToolTip();
            }
        }


        private void btnNext_Click(object sender, EventArgs e)
        {
            if (!bInitialized)
            {
                InitializePlayer();
            }
            if (gPlayer != null)
            {
                if ((gPlayer.Mode == Napster.PlayerMode.Shuffle) || (gPlayer.Mode == Napster.PlayerMode.ShuffleAndRepeat))
                {
                    gPlayer.Stop();
                    gPlayer.Play();
                }
                else
                {
                    gPlayer.Next();
                }
                InitializeToolTip();
            }
        }

        private void kh_KeyIntercepted(KeyboardHook.KeyboardHookEventArgs e)
        {
            //MessageBox.Show("e.key = " + e.KeyName);
        }

	}
}
